/**
 * <copyright>
 *
 * Copyright (c) 2015 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   IBM - Initial API and implementation
 *
 * </copyright>
 *
 * $Id$
 */
package org.eclipse.jet.internal.taglib.utils;

import java.util.ArrayList;
import java.util.List;

public class ConsoleTable {

  private String NL = System.getProperty("line.separator", "\n");

  private List<List> rows = new ArrayList<List>();

  private int colum;

  private int[] columLen;

  private static int margin = 2;

  private boolean printHeader = false;

  public ConsoleTable(int colum, boolean printHeader) {
    this.printHeader = printHeader;
    this.colum = colum;
    this.columLen = new int[colum];
  }

  public void appendRow() {
    List row = new ArrayList(colum);
    rows.add(row);
  }

  public ConsoleTable appendColum(Object value) {
    if (value == null) {
      value = "NULL";
    }
    List row = rows.get(rows.size() - 1);
    row.add(value);
    int len = value.toString().getBytes().length;
    if (columLen[row.size() - 1] < len)
      columLen[row.size() - 1] = len;
    return this;
  }

  public String toString() {
    StringBuilder buf = new StringBuilder();

    int sumlen = 0;
    for (int len : columLen) {
      sumlen += len;
    }
    if (printHeader)
      buf.append("|").append(printChar('=', sumlen + margin * 2 * colum + (colum - 1))).append(NL);
    else
      buf.append("|").append(printChar('-', sumlen + margin * 2 * colum + (colum - 1))).append(NL);
    for (int ii = 0; ii < rows.size(); ii++) {
      List row = rows.get(ii);
      for (int i = 0; i < colum; i++) {
        String o = "";
        if (i < row.size())
          o = row.get(i).toString();
        buf.append('|').append(printChar(' ', margin)).append(o);
        buf.append(printChar(' ', columLen[i] - o.getBytes().length + margin));
      }
      buf.append(NL);
      if (printHeader && ii == 0)
        buf.append("|").append(printChar('=', sumlen + margin * 2 * colum + (colum - 1))).append(NL);
      else
        buf.append("|").append(printChar('-', sumlen + margin * 2 * colum + (colum - 1))).append(NL);
    }
    return buf.toString();
  }

  private String printChar(char c, int len) {
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < len; i++) {
      buf.append(c);
    }
    return buf.toString();
  }

  public static void main(String[] args) {
//    ConsoleTable t = new ConsoleTable(4, true);
//    t.appendRow();
//    t.appendColum("序号").appendColum("姓名").appendColum("性别").appendColum("年龄");
//
//    t.appendRow();
//    t.appendColum("1").appendColum("张123213dadadssssssssssssssssssssssssssssssssssssssssssssssssssssssssss").appendColum("男").appendColum("11");
//
//    t.appendRow();
//    t.appendColum("22").appendColum("23123强3333");
////    System.out.printf("%-10s", t.toString());
    System.err.printf("%-8s\t\t%-8s\t\t%-8s\t\t%-8s\t\t%-8s\n","商品编号","商品名称","商品单价","单位","数量");
    System.err.printf("%-8s\t\t%-8s\t\t%-8s\t\t%-8s\t\t%-8s\n","小样", "大家好", "babd", "瓶", "def");   

   }

   public static boolean isChar(char c) {
    return (c + "").getBytes().length == 1 ? true : false;
   }
  
  
  
}
